package leung.java.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class SimpleDemo {
	
	private static  SessionFactory sessionFactory;
	
	@BeforeClass
	public static void beforeClass(){
		sessionFactory=	new Configuration().configure("leung/java/hibernate/hibernate.cfg.xml").buildSessionFactory();
	}
	
	@Test
	public void add(){
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		User user=new User();
		user.setAge(26);
		user.setBirthday(new Date());
		user.setGender(true);
		user.setUserName("daniel leung");
//		Card card=new Card();
//		card.setIdcard("sgsdgsgdsdg");
//		user.setCard(card);
		session.save(user);
		session.getTransaction().commit();
		System.out.println("insert succ");
	}
	
	public void remove(){
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.delete(new User(5));
		session.getTransaction().commit();
	}
	
	public void query(){
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		Query query=session.createQuery("from User");
		List<User> list=(List<User>)query.list();
		for(User user:list){
			user.print();
		}
		session.getTransaction().commit();
	}
	
	public void update(){
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.update(new User(5));
		session.getTransaction().commit();
	}
	
	
	public void scrollableResult(){
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		ScrollableResults results =session.getNamedQuery("").scroll();
		while(results.next()){
			
		}
	}
	
	
	public void batchInsert(){
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		for(int i=0;i<1000;i++){
			session.save(new User());
			if(i%30==0){//   jdbc batch_size:30
				session.flush();
				session.clear();
			}
		}
		
		session.getTransaction().commit();
	}
	
	
	
	public void batchUpdate(){
		
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
		// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
		int updatedEntities = session.createQuery( hqlUpdate )
		        .setString( "newName", "aa" )
		        .setString( "oldName", "bb" )
		        .executeUpdate();
		tx.commit();
		session.close();
	}
	
	
	public void batchDelete(){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		String hqlDelete = "delete Customer c where c.name = :oldName";
		// or String hqlDelete = "delete Customer where name = :oldName";
		int deletedEntities = session.createQuery( hqlDelete )
				.setString( "oldName", "VV" )
				.executeUpdate();
		tx.commit();
		session.close();
	}
	
	
	public void insertHql(){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		String hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...";
		int createdEntities = session.createQuery( hqlInsert )
		        .executeUpdate();
		tx.commit();
		session.close();
	}
	
	
	@AfterClass
	public static void afterClass(){
		System.out.println("afterclass");
	}
	

}
